package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.UserMapper;
import com.heima.admin.service.UserService;
import com.heima.model.admin.dto.AdUserDto;
import com.heima.model.admin.pojo.AdUser;
import com.heima.model.admin.vo.AdUserVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, AdUser>implements UserService {


    @Override
    public ResponseResult login(AdUserDto dto) {

        //检查参数   用户名和密码都必须存在
        if (StringUtils.isBlank(dto.getName())||StringUtils.isBlank(dto.getPassword())){

            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码不能为空");//一个是字符串
        }

        //检查用户状态是否有效
        AdUser adUser = getOne(Wrappers.<AdUser>lambdaQuery().eq(AdUser::getName, dto.getName()));

        if (adUser==null){//一个是具体的对象
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }



        //检测密码是否正确
        String pas = DigestUtils.md5DigestAsHex((dto.getPassword() + adUser.getSalt()).getBytes());
        if (!pas.equals(adUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        Integer status = adUser.getStatus();
        if (status!=9){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该用户状态异常");
        }
        //修改最近的登录时间
        adUser.setLoginTime(new Date());
        updateById(adUser);
        //颁发token
        String token = AppJwtUtil.getToken(adUser.getId());
        Map result = new HashMap<>();
        result.put("token",token);

        AdUserVo adUserVo = new AdUserVo();
        BeanUtils.copyProperties(adUser,adUserVo);//前面是原对象,后面是目标对象
        result.put("user",adUser);


        return ResponseResult.okResult(result);
    }

    @Override
    public ResponseResult register(AdUser adUser) {

        //检查参数
        if (StringUtils.isBlank(adUser.getName())||StringUtils.isBlank(adUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码不能为空");
        }
        //密码加密
        String salt = RandomStringUtils.randomAlphanumeric(8);//8位
        String password = adUser.getPassword();

        String newPassword = DigestUtils.md5DigestAsHex((password + salt).getBytes());

        adUser.setPassword(newPassword);
        adUser.setSalt(salt);
        adUser.setCreatedTime(new Date());
        adUser.setStatus(9);
        save(adUser);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
